﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domino;

namespace Streambolics.Notes
{
    public class Document
    {
        private NotesDocument _Document;
        private string _Name;

        internal protected Document (NotesDocument aDocument)
        {
            _Document = aDocument;
            _Name = FirstItemText ("Unnamed document", "Name", "Title", "Description", "ShortDesc", "Type");
        }

        public override string ToString ()
        {
            return _Name;
        }

        public string ItemText (string aItemName, string aDefault)
        {
            var item = _Document.GetFirstItem (aItemName);
            if (item != null)
            {
                return item.Text;
            }
            else
            {
                return aDefault;
            }
        }

        public string ItemText (string aItemName)
        {
            return ItemText (aItemName, String.Empty);
        }

        public string FirstItemText (string aDefault, params string[] aNames)
        {
            foreach (string name in aNames)
            {
                string val = ItemText (name);
                if (!String.IsNullOrEmpty (val))
                {
                    return val;
                }
            }
            return aDefault;
        }

        public string Type
        {
            get
            {
                return FirstItemText ("Untyped document", "Type", "TypeField");
            }
        }

        public string Form
        {
            get
            {
                return ItemText ("Form");
            }
        }

        public IEnumerable<string> ItemNames
        {
            get
            {
                var items = (object[])_Document.Items;
                foreach (object itemobj in items)
                {
                    NotesItem i = (NotesItem)itemobj;
                    yield return i.Name;
                }
            }
        }
    }
}
